package com.zhongya.example.view

import android.content.Context
import android.graphics.*
import android.graphics.Color.RED
import android.util.AttributeSet
import android.view.View

class BaseView : View {

    constructor(context: Context) : super(context) {

    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {

    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)



//        drawTuXing(canvas)

//        drawLuJing(canvas)
//        drawQuYu(canvas)

        testCanvas(canvas)


    }

    /*
    *画布
    * */

    private fun testCanvas(canvas: Canvas?) {

        /*val paint = Paint()
        paint.color = RED
        paint.style = Paint.Style.FILL

        canvas?.translate(100f,100f)

        val rect = Rect(0,0,400,200)
        canvas?.drawRect(rect,paint)*/

        val paint_green = generatePaint(Color.GREEN, Paint.Style.STROKE,3f)
        val paint_red = generatePaint(Color.RED, Paint.Style.STROKE,3f)
        val paint_yellow = generatePaint(Color.YELLOW, Paint.Style.STROKE,3f)

        val rect1 = Rect(0,0,400,200)
        canvas?.drawRect(rect1,paint_green)
        canvas?.translate(100f,100f)
        canvas?.drawRect(rect1,paint_red)
        canvas?.translate(100f,100f)
        canvas?.drawRect(rect1,paint_yellow)


    }

    fun generatePaint(color: Int, style: Paint.Style, width: Float): Paint{
        val paint = Paint()
        paint.color = color
        paint.style = style
        paint.strokeWidth = width
        return paint
    }

    /*
    * 基本图形绘制
    * */

    fun drawTuXing(canvas: Canvas?) {
        val paint = Paint()
        paint.color = RED
        paint.style = Paint.Style.FILL
//        paint.style = Paint.Style.STROKE
//        paint.style = Paint.Style.FILL_AND_STROKE
        paint.strokeWidth = 10f


        /*
        * 画圆
        * */
//        canvas?.drawCircle(180f, 250f, 150f, paint)
//        paint.color = 0x7EFFFF00
//        canvas?.drawCircle(180f, 250f, 100f, paint)

        /*
        * 画背景
        * */
//        canvas?.drawRGB(255,0,255)//十进制
//        canvas?.drawRGB(0xff,0,0xff)//十六进制

        /*
        * 画直线
        * */

//        canvas?.drawLine(100f,100f,200f,200f,paint)

        /*
        * 画点
        * */
//        canvas?.drawPoint(100f,100f,paint)

        /*
        * 矩形
        * */
        //直接构造
//        canvas?.drawRect(10f,10f,100f,100f,paint)
        //使用rectF构造
//        paint.style = Paint.Style.STROKE
//        val rect = Rect(10,10,100,100)
//        canvas?.drawRect(rect,paint)
    }

    /*
    * 绘制路径
    * */
    fun drawLuJing(canvas: Canvas?) {


        val paint = Paint()
        paint.color = RED
        paint.style = Paint.Style.STROKE
        paint.strokeWidth = 10f

        //直线路径
        /*val path = Path()
        path.moveTo(10f,10f)
        path.lineTo(10f,100f)
        path.lineTo(300f,100f)
        path.close() //闭环
        canvas?.drawPath(path,paint)*/

        //弧线路径
        val path = Path()
        path.moveTo(10f,10f)
        val rectF= RectF(100f,10f,200f,100f)
        path.arcTo(rectF,0f,90f,true)
        canvas?.drawPath(path,paint)
    }

    /*
    * region
    * (区域)
    * */
    fun drawQuYu(canvas: Canvas?) {
        val paint = Paint()
        paint.color = RED
        paint.style = Paint.Style.FILL

        //矩形
//        val region = Region(Rect(50,50,200,100))
//        drawRegion(canvas,region,paint)

        //构造一条椭圆路径
        /*val path = Path()
        val rect = RectF(50f,50f,200f,500f)
        path.addOval(rect,Path.Direction.CCW)
        val region = Region()
        region.setPath(path, Region(50,50,200,200))
        drawRegion(canvas,region,paint)*/

        //区域相交
        /*val region = Region(10,10,200,100)
        region.union(Rect(10,10,50,300))
        drawRegion(canvas,region,paint)*/

        //
        val rect1 = Rect(100,100,400,200)
        val rect2 = Rect(200,0,300,300)

        paint.style = Paint.Style.STROKE
        paint.strokeWidth = 2f
        canvas?.drawRect(rect1,paint)
        canvas?.drawRect(rect2,paint)

        val region1 = Region(rect1)
        val region2 = Region(rect2)
        region1.op(region2, Region.Op.XOR)

        val paint2 = Paint()
        paint2.color = Color.GREEN
        paint2.style = Paint.Style.FILL
        drawRegion(canvas,region1,paint2)


    }

    fun drawRegion(canvas: Canvas?, region: Region, paint: Paint){
        val iter = RegionIterator(region)
        val r = Rect()
        while (iter.next(r)){
            canvas?.drawRect(r,paint)
        }

    }

}